package com.xtpt.disaster.utils;

import com.alibaba.fastjson.JSONObject;
import com.xtpt.disaster.common.PropertyConfig;
import org.apache.commons.httpclient.HttpClient;
import org.apache.commons.httpclient.NameValuePair;
import org.apache.commons.httpclient.methods.PostMethod;
import org.apache.commons.httpclient.methods.RequestEntity;
import org.apache.commons.httpclient.methods.StringRequestEntity;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.UnsupportedEncodingException;
import java.util.Map;

/**
 *  POST请求定制方法类，模拟浏览器请求头
 *
 *  HZJ
 */
public class PostUtil {

    public static String postRequestWithMap(String url, Map<String,String> map, Map<String, String> header, String charset) {

        HttpClient client = new HttpClient();
        PostMethod postMethod = new PostMethod(url);
        //设置请求体 body
        if (map != null){
            NameValuePair[] data = new NameValuePair[map.size()];
            int mapCount = 0;
            for (String s : map.keySet()) {
                data[mapCount] = new NameValuePair(s, map.get(s));
                mapCount++;
            }
            postMethod.setRequestBody(data);
        }

        //设置请求头
        if (header != null){
            for (String s : header.keySet()) {
                postMethod.setRequestHeader(s,header.get(s));
            }
        }

        postMethod.setRequestHeader(
                "User-Agent",
                "Mozilla/5.0 (Windows; U; Windows NT 5.1; zh-CN; rv:1.9.1.2) Gecko/20090729 Firefox/3.5.2");
        postMethod.setRequestHeader("Accept",
                "text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8");
        try {
            client.executeMethod(postMethod);
            StringBuffer response = new StringBuffer();
            BufferedReader reader = new BufferedReader(new InputStreamReader(
                    postMethod.getResponseBodyAsStream(), "utf-8"));//以gb2312编码方式打印从服务器端返回的请求
            String line;
            while ((line = reader.readLine()) != null) {
                response.append(line).append(
                        System.getProperty("line.separator"));
            }
            reader.close();

            return response.toString();
        } catch (IOException e) {
            e.printStackTrace();
        }

        return null;
    }

    public static String postRequestWithJSON(String url, JSONObject jsonObject, Map<String, String> header, String charset) {

        HttpClient client = new HttpClient();
        PostMethod postMethod = new PostMethod(url);
        //设置请求体 body
        if (jsonObject != null){
            try {
                String tmp = jsonObject.toJSONString();
                RequestEntity se = new StringRequestEntity(tmp ,"application/json" ,"UTF-8");
                postMethod.setRequestEntity(se);
            } catch (UnsupportedEncodingException e) {
                e.printStackTrace();
            }
        }

        //设置请求头
        if (header != null){
            for (String s : header.keySet()) {
                postMethod.setRequestHeader(s,header.get(s));
            }
        }

        postMethod.setRequestHeader(
                "User-Agent",
                "Mozilla/5.0 (Windows; U; Windows NT 5.1; zh-CN; rv:1.9.1.2) Gecko/20090729 Firefox/3.5.2");
        postMethod.setRequestHeader("Accept",
                "text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8");
//        postMethod.setRequestHeader("Content-Type","application/json;charset=UTF-8");
        try {
            client.executeMethod(postMethod);
            StringBuffer response = new StringBuffer();
            BufferedReader reader = new BufferedReader(new InputStreamReader(
                    postMethod.getResponseBodyAsStream(), "utf-8"));//以gb2312编码方式打印从服务器端返回的请求
            String line;
            while ((line = reader.readLine()) != null) {
                response.append(line).append(
                        System.getProperty("line.separator"));
            }
            reader.close();
            return response.toString();

        } catch (IOException e) {
            e.printStackTrace();
        }
        return null;
    }
}
